﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EStore.Core.Orders;
using System.Diagnostics.Contracts;
using Project.Util.UAE;
using Project.Util;

namespace EStore.Core.Customers
{
    public class Customer : IAggregateRoot<Customer, int, ICustomerRepository>
    {
        static ICustomerRepository _repository
        {
            get
            {
                return EngineContext.Current.ContainerManager.Resolve<ICustomerRepository>();
            }
        }

        public int ID { get; set; }

        public string UserName { get; set; }

        public string Password { get; set; }

        public static Customer SignUp(string userName, string password)
        {
            Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(userName), "用户名不能为空");
            Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(password), "密码不能为空");

            if (FindByUserName(userName) != null)
                return null;

            return new Customer { UserName = userName, Password = password.Md5() };
        }

        public static Customer FindByUserName(string userName)
        {
            Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(userName), "用户名不能为空");
            return _repository.FindByUserName(userName);
        }

        IOrderFactory orderFactory;
        public Order PlaceOrder()
        {
            var order = new OrderFactory().CreateOrder();
            //TODO 填充订单信息
            return order;
        }

        public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        {
            throw new NotImplementedException();
        }

        public bool CanBeSaved
        {
            get { throw new NotImplementedException(); }
        }

        //public static Customer Null = 

    }
}
